layui.config({
    base: '/static/treetable-lay/'
}).use(['form','layer','jquery','treetable','table','laydate','upload','element'],function(){
    var form = layui.form,
        layer = layui.layer,
        laypage = layui.laypage,
        $ = layui.jquery,
        table=layui.table,
        upload = layui.upload,
        laydate=layui.laydate,
        element=layui.element,
        treetable = layui.treetable;
    
    var gDeptId = undefined;
    // 渲染表格
    function reload(){
    	treetable.render({
    		treeColIndex: 1,
    		treeSpid: -1,
    		treeIdName: 'id',
    		treePidName: 'pid',
    		treeDefaultClose: false,
    		treeLinkage: false,
    		elem: '#deptTable',
    		url: '/department/list',
    		page: false,
    		height: 'full-30',
    		cols: [[
    			{type: 'radio',width:"5%"},
    			{field: 'deptCode', title: '部门编码',width:"45%"},
    			{field: 'deptName', title: '部门名称',width:"50%"},
    			]],
    			defaultToolbar: [],
    			toolbar: '#tableToolbar'
    	});
    }
    reload();
	table.on('toolbar(deptTable)', function(obj){
    	var tableCheckData = table.checkStatus(obj.config.id).data;
		switch(obj.event){
		    case 'add_base':
		    	var index = layer.open({
		    		type: 2
		    		,title: "新增根部门"
		    		,maxmin: true
		    	  	,area: ['40%', '60%']
		    	  	,content: '/department/addHtml'
		    	  	,btn: ['保存','返回']
		        	,yes: function(index, layero){
		        		var body = layer.getChildFrame('body', index);
		        		
		        		var pid = body.find("#pid").val();
		        		var deptCode = body.find("#deptCode").val();
		        		var deptName = body.find("#deptName").val();
		        		
		        		if(deptCode == ''){
		        			layer.msg("部门编码不能为空",{icon:2});
		        			return;
		        		}
		        		if(deptName == ''){
		        			layer.msg("部门名称不能为空",{icon:2});
		        			return;
		        		}
		        		var addData={
		        			pid:pid,
		        			deptCode:deptCode,
		        			deptName:deptName
		        		}
		        		
		        		$.post('/department/add',addData,function(d){
		        			if(d.code == '200'){
		        				top.layer.msg("新增成功",{icon:1});
								layer.close(index);
							}else{
								layer.msg("新增失败，"+d.msg,{icon:2});
							}
		        			reload();
		        		});
		        	}
	        	})
        	break;
		    case 'add_sub':
		    	if(gDeptId==undefined){
		    		layer.msg("请选择节点");
		    		return;
		    	}
		    	var index = layer.open({
		    		type: 2
		    		,title: "新增子部门"
		    		,maxmin: true
		    	  	,area: ['40%', '60%']
		    	  	,content: '/department/addHtml?id='+gDeptId
		    	  	,btn: ['保存','返回']
		        	,yes: function(index, layero){
		        		var body = layer.getChildFrame('body', index);
		        		
		        		var pid = body.find("#pid").val();
		        		var deptCode = body.find("#deptCode").val();
		        		var deptName = body.find("#deptName").val();
		        		
		        		if(deptCode == ''){
		        			layer.msg("部门编码不能为空",{icon:2});
		        			return;
		        		}
		        		if(deptName == ''){
		        			layer.msg("部门名称不能为空",{icon:2});
		        			return;
		        		}
		        		var addData={
		        			pid:pid,
		        			deptCode:deptCode,
		        			deptName:deptName
		        		}
		        		
		        		$.post('/department/add',addData,function(d){
		        			if(d.code == '200'){
		        				top.layer.msg("新增成功",{icon:1});
								layer.close(index);
								gDeptId = undefined;
							}else{
								layer.msg("新增失败，"+d.msg,{icon:2});
							}
		        			reload();
		        		});
		        	}
	        	})
        	break;
		    case 'edit':
		    	if(gDeptId==undefined){
		    		layer.msg("请选择修改数据");
		    		return;
		    	}
		    	var index = layer.open({
		    		type: 2
		    		,title: "修改"
		    		,maxmin: true
		    	  	,area: ['40%', '60%']
		    	  	,content: '/department/editHtml?id='+gDeptId
		    	  	,btn: ['保存','返回']
		        	,yes: function(index, layero){
		        		var body = layer.getChildFrame('body', index);
		        		
		        		var pid = body.find("#pid").val();
		        		var deptCode = body.find("#deptCode").val();
		        		var deptName = body.find("#deptName").val();
		        		
		        		if(deptCode == ''){
		        			layer.msg("部门编码不能为空",{icon:2});
		        			return;
		        		}
		        		if(deptName == ''){
		        			layer.msg("部门名称不能为空",{icon:2});
		        			return;
		        		}
		        		var editData={
	        				id:gDeptId,
	        				pid:pid,
		        			deptCode:deptCode,
		        			deptName:deptName
		        		}
		        		
		        		$.post('/department/edit',editData,function(d){
		        			if(d.code == '200'){
		        				top.layer.msg("修改成功",{icon:1});
								layer.close(index);
								gDeptId = undefined;
							}else{
								layer.msg("修改失败，"+d.msg,{icon:2});
							}
		        			reload();
		        		});
		        	}
	        	})
        	break;
		    case 'del':
		    	if(gDeptId == undefined){
					layer.msg("请选择删除数据");
					return;
		    	}
		    	top.layer.confirm('是否删除?如果当前节点下有子节点，将会被全部删除！', {icon: 3, title:'提示'}, function(index){
			    	var shadeIndex = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.4});
			    	$.post('/department/delete',{id:gDeptId},function(d){
			    		if(d.code == '200'){
							top.layer.msg("删除成功",{icon:1});
							gDeptId = undefined;
						}else{
							top.layer.msg("删除失败",{icon:2});
						}
						top.layer.close(shadeIndex);
						reload();
			    	},'json')
		    		top.layer.close(index);
		    	});
		    break;
		};
	})
	table.on('row(deptTable)', function(obj){
    	obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');//选中行样式
        obj.tr.find('input[lay-type="layTableRadio"]').prop("checked",true);
        form.render('radio');
    	gDeptId = obj.data.id;
	});
})
